package com.ajax.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;

import virtuoso.jena.driver.VirtGraph;
import virtuoso.jena.driver.VirtuosoQueryExecution;
import virtuoso.jena.driver.VirtuosoQueryExecutionFactory;

/**
 * Servlet implementation class overviewServlet
 */
public class overviewServlet extends HttpServlet {

       
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 
		response.setContentType("text/html");

		//String str = request.getParameter("ta");
		//String num2 = request.getParameter("num2");
		
		String url,json="";
	//	String t = null;
	    url = "jdbc:virtuoso://localhost/:1111/charset=UTF-8/log_enable=2";
//202.114.255.99:1111
	VirtGraph graph = new VirtGraph ("http://wust.edu.cn/Emergency",url, "dba", "dba");
// System.out.println( graph.getCount());
//System.out.println(graph.getGraphName());
   // String str = request.getParameter("ta");
	//String str="SELECT ?t  ?o WHERE  { ?t rdfs:subClassOf ?o.}" ;
try{	Query sparql = QueryFactory.create("PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n "
			+ "PREFIX Property:<http://www.wust.edu.cn/emergency/property#>\n"
			+ "PREFIX Emr:<http://www.wust.edu.cn/emergency/>\n"
			+ "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>\n"+
			"PREFIX owl:<http://www.w3.org/2002/07/owl#>\n"
			+"select ?a ?b ?c where{ ?a rdf:type ?b. ?a Property:location ?c}");
	

	VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, graph);
	ResultSet results = vqe.execSelect();
	
	

	//ResultSetFormatter.outputAsJSON(results);;
	// ResultSetMetaData m=null;
	//int s=0;
	//JSONArray array = new JSONArray(); 
	json = "[";
	List<String> r = results.getResultVars();
	
//	for(int i=0;i<r.size()-2;i++){
//		json += "\"H"+i+"\":\""+r.get(i)+"\"";
//	//System.out.println(r.get(i));
//	}
//	if(r.size()-1==1){
//		json+="{\"H0\":"+"\""+r.get(0)+"\"},";
//	}
//	else if(r.size()-1==2){
//		json+="{\"H0\":"+"\""+r.get(0)+"\",\"H1\":"+"\""+r.get(1)+"\"},";
//	}
	//else if(r.size()-1==3){
	json+="{\"H\":\"序号\",\"H0\":"+"\""+"标题"+"\",\"H1\":"+"\""+"类型"+"\",\"H2\":"+"\""+"地点"+"\"},";
	//}

	//json+="\n\"data\":";
	int j=0;
	while (results.hasNext()) {
		QuerySolution result = results.nextSolution();
		
		j++;
//		for(int i=0;i<r.size()-1;i++){
//		json += result.get(r.get(i).toString()).toString();
//	  }

	String s1 = result.get(r.get(0).toString()).toString();
	String s2 = result.get(r.get(1).toString()).toString();
	String s3 = result.get(r.get(2).toString()).toString();
			json+="{\"H\":"+j+",\""+r.get(0).toString()+"\":"+"\""+s1.substring(33,s1.length())
					+"\",\""+r.get(1).toString()+"\":"+"\""+s2.substring(33,s2.length())+"\","
							+ "\""+r.get(2).toString()+"\":"+"\""+s3.substring(33,s3.length())+"\"},\n";
		
		
		//json+="\n";
	}//while
	//j就是结果行数
	if(j!=0){
	//String json1 = "abcde";
	json=json.substring(0, json.length()-2);
	json = json+",{\"sum\":"+j+"}";
	json+="]";
	}
	else{
		json = json+",{\"sum\":"+j+"}";
		json+="]";
	}
	vqe.close();
}catch(Exception e){
	json = "fail";
	System.out.println(e.getMessage());
}
	//System.out.println("graph.getCount() = " + graph.getCount());

	
	 

		
		PrintWriter out = response.getWriter();
	//	System.out.println(json);
		
	//	System.out.println("AjaxServlet.doGet()");
	//	System.out.println(p);
		out.println(json);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
	//	System.out.println("AjaxServlet.doPost()");
		
		this.doGet(request, response);
	}

}
